var express = require('express');
var router = express.Router();
var multer = require('multer')

// 图片上传
var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'public/images/novels')
  },
  filename: function (req, file, cb) {
    console.log(file)
    let oname = file.originalname.split('.');
    let len = oname.length;
    let ext = oname[len-1]; //图片后缀
    let imgname = new Date().getTime() + parseInt(Math.random() * 9999)
    cb(null, `${imgname}.${ext}`)
  }
})

var upload = multer({storage:storage});

router.post('/novel/upload', upload.single('novelimg'),function(req, res, next) {
  res.send({
    code:1,
    msg:'上传成功',
    imgSrc:`/public/images/novels/${req.file.filename}`
  })
});



module.exports = router;
